package com.nowcoder;

import java.util.Arrays;
import java.util.Scanner;

public class SortScores {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            int n = scanner.nextInt();
            boolean isAsc = scanner.nextInt() == 1;
            scanner.nextLine();
            Score[] scores = new Score[n];
            for (int i = 0; i < n; i++) {
                scores[i] = new Score(scanner.nextLine());
            }
            // 打印输出结果
            solver(scores, isAsc);
        }
    }

    public static void solver(Score[] scores, boolean isAsc) {
        Arrays.sort(scores, (o1, o2) -> {
            return isAsc ? o1.grade - o2.grade : o2.grade - o1.grade;
        });
        for (int i = 0; i < scores.length; i++) {
            System.out.println(scores[i]);
        }
    }
}

class Score {
    String name;
    int grade;

    public Score(String line) {
        String[] strs = line.split(" ");
        name = strs[0];
        grade = Integer.valueOf(strs[1]);
    }

    public String toString() {
        return name + " " + grade;
    }
}
